﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Millionaire;
using System.Data.Linq;

namespace WindowsFormsApplication1.BLL
{
    public class CauHoiBLL : BaseBLL, API.ICauHoiBLL
    {
        #region ICauHoiBLL Members

        public bool AddNew(CauHoi c)
        {
            using (var ctxt = new MillionaireDataContext(connString))
            {
                try
                {
                    ctxt.CauHois.InsertOnSubmit(c);
                    ctxt.SubmitChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }

        public bool Update(CauHoi c)
        {
            using (var ctxt = new MillionaireDataContext(connString))
            {
                try
                {
                    CauHoi edit = ctxt.CauHois.SingleOrDefault(x => x.MaCauHoi == c.MaCauHoi);
                    if (edit != null)
                    {
                        edit.CauHoi1 = c.CauHoi1;
                        edit.CauA = c.CauA;
                        edit.CauB = c.CauB;
                        edit.CauC = c.CauC;
                        edit.CauD = c.CauD;
                        edit.DapAn = c.DapAn;
                        edit.MaLinhVuc = c.MaLinhVuc;
                        edit.MaLinhVuc = c.MaLuaTuoi;
                        ctxt.SubmitChanges();
                        return true;
                    }
                    else
                        return false;

                }
                catch (Exception)
                {
                    return false;
                }
            }
        }

        public bool Delete(CauHoi c)
        {
            using (var ctxt = new MillionaireDataContext(connString))
            {
                try
                {
                    CauHoi delObj = ctxt.CauHois.SingleOrDefault(x => x.MaCauHoi == c.MaCauHoi);
                    if (delObj != null)
                    {
                        ctxt.CauHois.DeleteOnSubmit(delObj);
                        ctxt.SubmitChanges();
                        return true;
                    }
                    else
                        return false;

                }
                catch (Exception)
                {
                    return false;
                }
            }
        }

        public CauHoi GetByID(int idCauHoi)
        {
            using (var ctxt = new MillionaireDataContext(connString))
            {
                ctxt.ObjectTrackingEnabled = false;
                return ctxt.CauHois.SingleOrDefault(x => x.MaCauHoi == idCauHoi);
            }
        }

        public List<CauHoi> GetAll()
        {
            using (var ctxt = new MillionaireDataContext(connString))
            {
                ctxt.ObjectTrackingEnabled = false;
                ctxt.DeferredLoadingEnabled = false;
                DataLoadOptions dlo = new DataLoadOptions();
                dlo.LoadWith<CauHoi>(x=>x.LinhVuc);
                dlo.LoadWith<CauHoi>(x => x.LuaTuoi);
                ctxt.LoadOptions = dlo;
                return ctxt.CauHois.OrderBy(x=>x.MaLinhVuc).ThenBy(x=>x.MucDo).ToList();
            }
        }

        public List<CauHoi> GetByLinhVuc(int idLV)
        {
            using (var ctxt = new MillionaireDataContext(connString))
            {
                ctxt.ObjectTrackingEnabled = false;
                return ctxt.CauHois.Where(x=>x.MaLinhVuc == idLV).ToList();
            }
        }

        public List<CauHoi> GetByLinhVuc(List<int> dsIdLV)
        {
            using (var ctxt = new MillionaireDataContext(connString))
            {
                ctxt.ObjectTrackingEnabled = false;
                return ctxt.CauHois.Where(x => dsIdLV.Contains(x.MaLinhVuc.Value)).ToList();
            }
        }

        public List<CauHoi> GetByLuaTuoi(int idLuaTuoi)
        {
            using (var ctxt = new MillionaireDataContext(connString))
            {
                ctxt.ObjectTrackingEnabled = false;
                return ctxt.CauHois.Where(x => x.MaLuaTuoi == idLuaTuoi).ToList();
            }
        }

        #endregion

        public CauHoi Random()
        {
            using (var ctxt = new MillionaireDataContext(connString))
            {
                Random random = new Random();
                ctxt.ObjectTrackingEnabled = false;
                return ctxt.CauHois.ToList().OrderBy(x => random.Next()).First();
            }
        }
        public CauHoi RandomMD(int mucDo)
        {
            using (var ctxt = new MillionaireDataContext(connString))
            {
                Random random = new Random();
                ctxt.ObjectTrackingEnabled = false;
                return ctxt.CauHois.Where(x => x.MucDo == mucDo).ToList().OrderBy(x => random.Next()).First();
            }
        }
        public CauHoi RandomLV(int linhVuc,int mucDo)
        {
            using (var ctxt = new MillionaireDataContext(connString))
            {
                Random random = new Random();
                ctxt.ObjectTrackingEnabled = false;
                return ctxt.CauHois.Where(x => x.MucDo == mucDo && x.MaLinhVuc == linhVuc).ToList().OrderBy(x => random.Next()).First();
            }
        }
        public CauHoi RandomNhieuLV(List<int> linhVuc, int mucDo)
        {
            using (var ctxt = new MillionaireDataContext(connString))
            {
                Random random = new Random();
                ctxt.ObjectTrackingEnabled = false;
                return ctxt.CauHois.Where(x => x.MucDo == mucDo && linhVuc.Contains(x.MaLinhVuc.Value)).ToList().OrderBy(x => random.Next()).First();
            }
        }


        public CauHoi DoiCauHoi(List<int> linhVuc, int mucDo,int currCauHoi)
        {
            using (var ctxt = new MillionaireDataContext(connString))
            {
                Random random = new Random();
                ctxt.ObjectTrackingEnabled = false;
                try
                {
                    return ctxt.CauHois.Where(x => x.MucDo == mucDo && linhVuc.Contains(x.MaLinhVuc.Value) && x.MaCauHoi != currCauHoi).ToList().OrderBy(x => random.Next()).First();
                }
                catch (Exception)
                {
                    return ctxt.CauHois.Where(x => x.MucDo == mucDo && x.MaCauHoi != currCauHoi).ToList().OrderBy(x => random.Next()).First();
                }
              
            }
        }
        public CauHoi DoiCauHoi(int mucDo, int currCauHoi)
        {
            using (var ctxt = new MillionaireDataContext(connString))
            {
                Random random = new Random();
                ctxt.ObjectTrackingEnabled = false;
               
                return ctxt.CauHois.Where(x => x.MucDo == mucDo && x.MaCauHoi != currCauHoi).ToList().OrderBy(x => random.Next()).First();
   
            
              
            }
        }
      

       
    }
}
